package com.itheima.dao.system;

import com.itheima.domain.system.Role;
import com.itheima.domain.system.SystemSet;
import com.itheima.domain.system.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 实现用户注册，用户登录
 * 2021/10/15 15:09
 * created by guowenliang
 */
public interface UserMapper {

    @Select("select * from ss_user where user_name=#{userName} and password=#{password}")
    List<User> findUser(User user);

    @Insert("insert into ss_user (picture,user_id,user_name,password,email) values ('/img/user2-160x160.jpg',#{userId},#{userName},#{password},#{email})")
    void register(User user);

    //    @Select("select * from ss_user order by update_time desc")
    List<User> findAll();

    @Select("select * from ss_role_user where user_id=#{userId}")
    List<User> findUserAndRole(@Param("userId") String userId);

    @Insert("insert into ss_user " +
            "(user_id,user_name,password,email,dept_id,dept_name,gender,birthday,state,telephone,join_date,create_time,update_time)" +
            "values (#{userId},#{userName},#{password},#{email},#{deptId},#{deptName},#{gender},#{birthday},#{state},#{telephone},#{joinDate},now(),now())")
    void addUser(User user);

    @Update("update ss_user set " +
            "user_name=#{userName},email=#{email},dept_id=#{deptId},dept_name=#{deptName},gender=#{gender},birthday=#{birthday},state=#{state},telephone=#{telephone},join_date=#{joinDate},update_time=now()" +
            "where user_id=#{userId}")
    void updateUser(User user);


    @Delete("delete from ss_user where user_id=#{userId}")
    void delete(@Param("userId") String id);

    @Select("select * from ss_user where user_id=#{userId}")
    User findById(@Param("userId") String id);

    /**
     * 查询所有的角色，若用户拥有的角色，前端默认勾选
     * @param userId 用户id
     * @return
     */
    List<Role> userRoleList(@Param("userId") String userId);

    /**
     * 通过用户id，删除用户角色中间表所有该用户的数据
     * @param userId 用户id
     */
    @Delete("delete from ss_role_user where user_id=#{userId}")
    void deleteRole(@Param("userId") String userId);

    /**
     * 添加用户角色中间表数据
     * @param userId 用户id
     * @param roleId 角色id
     */
    @Insert("insert into ss_role_user (user_id,role_id) values (#{userId},#{roleId})")
    void addRole(@Param("userId") String userId, @Param("roleId") String roleId);

    @Select("select * from ss_user where user_name=#{userName}")
    User findUserByUsername(@Param("userName") String userName);

    @Update("update ss_user set picture=#{picture} where user_id=#{userId}")
    void UploadPicture(User user);

    @Select("SELECT user_name,PASSWORD FROM ss_user WHERE user_name LIKE #{username} AND PASSWORD LIKE #{password}")
    User findUserAndPassword(@Param("username") String username, @Param("password") String password);

    @Update("UPDATE ss_user SET PASSWORD=#{password} WHERE user_name LIKE #{username} ")
    void updatePassword(@Param("username") String username, @Param("password") String password);

    List<User> findUserByCondition(@Param("searchUserName") String searchUserName,
                                   @Param("searchEmail") String searchEmail,
                                   @Param("searchDept") String searchDept,
                                   @Param("searchState") String searchState,
                                   @Param("searchGender") String searchGender);

    @Select("SELECT * FROM ss_user WHERE user_name LIKE #{username} AND email LIKE #{email}")
    User findUserByNameAndEmail(@Param("username") String username, @Param("email") String email);

    @Update("UPDATE ss_user SET PASSWORD=#{password} WHERE user_name LIKE #{username} AND email LIKE #{email}")
    void updatePasswordByNameAndEmail(@Param("username") String username, @Param("email") String email, @Param("password") String password);

    @Select("select * from ss_system_set order")
    List<SystemSet> findSystemSet();

    @Update("update ss_system_set set page_size=#{pageSize},update_time=now() where module_name=#{moduleName}")
    void updatePageSize(@Param("moduleName") String moduleName,
                        @Param("pageSize") String moduleNamePageSize);

    @Select("select * from ss_system_set where module_name=#{moduleName}")
    SystemSet findSystemSetByModuleName(@Param("moduleName") String moduleName);

}
